/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.retail.biz.log.service.impl;

import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.retail.biz.log.mapper.YxStorePurchaseLogMapper;
import co.yixiang.retail.biz.log.service.YxStorePurchaseLogService;
import co.yixiang.retail.common.domain.YxStorePurchaseLog;
import co.yixiang.retail.common.dto.YxStorePurchaseLogDto;
import co.yixiang.retail.common.dto.YxStorePurchaseLogQueryCriteria;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author poppu_hu
 * @date 2021-04-21
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxStorePurchaseLog")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStorePurchaseLogServiceImpl extends BaseServiceImpl<YxStorePurchaseLogMapper, YxStorePurchaseLog> implements YxStorePurchaseLogService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxStorePurchaseLogDto> queryAll(YxStorePurchaseLogQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStorePurchaseLog> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxStorePurchaseLogDto.class);
    }


    @Override
    //@Cacheable
    public List<YxStorePurchaseLog> queryAll(YxStorePurchaseLogQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStorePurchaseLog.class, criteria));
    }


    @Override
    public void download(List<YxStorePurchaseLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStorePurchaseLogDto yxStorePurchaseLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("门店id", yxStorePurchaseLog.getStoreId());
            map.put("门店名称", yxStorePurchaseLog.getStoreName());
            map.put("手机号", yxStorePurchaseLog.getPhoneNum());
            map.put("产品id", yxStorePurchaseLog.getProId());
            map.put("产品名称", yxStorePurchaseLog.getProName());
            map.put("订单id", yxStorePurchaseLog.getOrderId());
            map.put("产品数量", yxStorePurchaseLog.getProNum());
            map.put("产品价格", yxStorePurchaseLog.getProPrice());
            map.put("添加时间", yxStorePurchaseLog.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
